/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Rev_Trust.java
 *
 * Created on Mar 1, 2009, 2:48:15 PM
 */

/**
 *
 * @author Himanshu  Sharma
 */
import java.util.Random ;
import java.awt.event.MouseListener ;
import java.awt.event.MouseEvent ;
import java.awt.event.InputEvent ;

public class Rev_Trust extends javax.swing.JFrame {

    double money_expected = 0.0, p1_money_passed = 0.0, p1_init_money =12.0 ;
    double money_passed =0.0, p1_money_expects =0.0, money_won = 0.0;
    boolean count = false ;
    Game G;
    String stage = "1" ;

    /** Creates new form Rev_Trust */
    public Rev_Trust() {
        p1_init_money =12.0 ;
        initComponents();
        G = new Game() ;
        l3.setText(Double.toString(p1_init_money));
        this.setVisible(true);
        S1() ;
        addMouseListener( new Rev_Trust.CheckMouse());
        t1.setText("0.0"); t2.setText("0.0"); t3.setText("0.0");
    }

        public Rev_Trust(double p1_init_money) {
        this.p1_init_money = p1_init_money;
        initComponents();
        G = new Game() ;
        l3.setText(Double.toString(p1_init_money));
        this.setVisible(true);
        S1() ;
        addMouseListener( new Rev_Trust.CheckMouse());
        t1.setText("0.0"); t2.setText("0.0"); t3.setText("0.0");
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        l1 = new javax.swing.JLabel();
        l2 = new javax.swing.JLabel();
        l4 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        l5 = new javax.swing.JLabel();
        l3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        t2 = new javax.swing.JTextField();
        l6 = new javax.swing.JLabel();
        t3 = new javax.swing.JTextField();
        b2 = new javax.swing.JButton();
        b1 = new javax.swing.JButton();
        l10 = new javax.swing.JLabel();
        l11 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        l1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l1.setText("You are Player 2");

        l2.setText("Player 1 has $");

        l4.setText("What would you like to give to player 2");

        l5.setText("How much do you expect the player 1 to pass ?");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Trust Player 2");

        l6.setText("How much do you think Player 2 Expects you to pass");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(206, 206, 206)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(l5)
                                    .addComponent(l4)
                                    .addComponent(l2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(l3, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                                    .addComponent(t2, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                                    .addComponent(t1, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(l6)
                                .addGap(18, 18, 18)
                                .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(16, 16, 16)))
                .addGap(63, 63, 63))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(224, 224, 224)
                .addComponent(l1)
                .addContainerGap(123, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addComponent(l1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l2)
                    .addComponent(l3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l5)
                    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l4)
                    .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l6)
                    .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        b2.setText("Give");
        b2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b2ActionPerformed(evt);
            }
        });

        b1.setText("Next");
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });

        l10.setBackground(new java.awt.Color(204, 204, 204));
        l10.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        l10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l10.setText("Left Click to increase value");
        l10.setOpaque(true);

        l11.setBackground(new java.awt.Color(204, 204, 204));
        l11.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        l11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l11.setText("Right Click when done.");
        l11.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(254, 254, 254)
                        .addComponent(b2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(b1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l11, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addComponent(l10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(160, 160, 160))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(b2)
                            .addComponent(b1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addComponent(l10)
                        .addGap(39, 39, 39)
                        .addComponent(l11)))
                .addContainerGap(171, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b2ActionPerformed
        money_passed = Double.parseDouble(t2.getText()) ;
        p1_money_expects = Double.parseDouble(t3.getText()) ;
        
        money_won = p1_money_passed - money_passed;

        G.writeLog("Trust_Player2_money_won $" + String.format("%.2f", money_won) +
                ", Player2_expected $" + String.format("%.2f", money_expected )+
                ", Money_Passed to P1: $" + String.format("%.2f", money_passed) +
                ", P1_thought_P2_expected $" + String.format("%.2f", p1_money_expects));
        
        this.setVisible(false);
        G.free = true;
/*
        quiz q = new quiz();
        q.setVisible(true);
        setVisible(false); // TODO add your handling code here:
*/


}//GEN-LAST:event_b2ActionPerformed

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b1ActionPerformed

        Random r = new Random();
        double frac_mon_passed = 0;
        frac_mon_passed = r.nextDouble(); // We'll use random values to find the money returned. Out of 10, 6 will be no money.

        money_expected = Double.parseDouble(t1.getText());
        p1_money_passed =  p1_init_money * frac_mon_passed;

        l4.setVisible(true);
        l6.setVisible(true);
        t2.setVisible(true);
        t3.setVisible(true);
        b2.setVisible(true);
        b1.setVisible(false);
        l5.setText("P1 passed $" + String.format("%.2f",p1_money_passed));
        t1.setVisible(false);

}//GEN-LAST:event_b1ActionPerformed

        @Override
        public synchronized void addMouseListener(MouseListener l) {
        super.addMouseListener(l);
        }

    class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
            if( stage.equals("1") && t1.isEditable()) {
                double temp = Double.parseDouble(t1.getText()) + 1.0 ;
                t1.setText(Double.toString(temp));
            }
            else if( stage.equals("2") && t2.isEditable()) {
                double temp = Double.parseDouble(t2.getText()) + 1.0 ;
                t2.setText(Double.toString(temp));
            }
            else if( stage.equals("3") && t3.isEditable() ){
                double temp = Double.parseDouble(t3.getText()) + 1.0 ;
                t3.setText(Double.toString(temp));
            }
            else if( stage.equals("4")){
                stage = "5" ;
                money_passed = Double.parseDouble(t2.getText()) ;
                p1_money_expects = Double.parseDouble(t3.getText()) ;
                money_won = p1_money_passed - money_passed;
                G.writeLog("Trust_Player2_money_won $" + String.format("%.2f", money_won) +
                    ", Player2_expected $" + String.format("%.2f", money_expected )+
                    ", Money_Passed to P1: $" + String.format("%.2f", money_passed) +
                    ", P1_thought_P2_expected $" + String.format("%.2f", p1_money_expects));

                setVisible(false);
                G.free = true;
            }
            else if( stage.equals("5") ){
            }
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {}
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                if( stage.equals("1") ) {
                    stage = "2" ;
                    Random r = new Random();
                    double frac_mon_passed = 0;
                    frac_mon_passed = r.nextDouble(); // We'll use random values to find the money returned. Out of 10, 6 will be no money.
                    money_expected = Double.parseDouble(t1.getText());
                    p1_money_passed =  p1_init_money * frac_mon_passed;
                    l5.setText("P1 passed $" + String.format("%.2f",p1_money_passed));
                    S2() ;
                }
                else if( stage.equals("2") ) {
                    stage = "3" ;
                    S3() ;
                }
                else if( stage.equals("3") ) {
                    stage = "4" ;
                    S4() ;
                }
            }
        }
    }


        public void S1(){
        t1.setVisible(true) ;
        t2.setVisible(false);
        t3.setVisible(false);
        t1.setEditable(true);
        t2.setEditable(false);
        t3.setEditable(false);
        l4.setVisible(false) ;
        l5.setVisible(true) ;
        l6.setVisible(false) ;
        b2.setVisible(false);
    }

    public void S2(){
        t1.setVisible(false) ;
        t2.setVisible(true);
        t3.setVisible(false);
        t1.setEditable(false);
        t2.setEditable(true);
        t3.setEditable(false);
        l4.setVisible(true) ;
        l5.setVisible(true) ;
        l6.setVisible(false) ;
        l2.setVisible(false) ;
        l3.setVisible(false) ;
        b2.setVisible(false);
    }

    public void S3(){
        t1.setVisible(false) ;
        t2.setVisible(false);
        t3.setVisible(true);
        t1.setEditable(false);
        t2.setEditable(false);
        t3.setEditable(true);
        l4.setVisible(false) ;
        l5.setVisible(false) ;
        l6.setVisible(true) ;
        l2.setVisible(false) ;
        l3.setVisible(false) ;
        b2.setVisible(false);
    }

    public void S4(){
        t1.setVisible(false) ;
        t2.setVisible(false);
        t3.setVisible(false);
        t1.setEditable(false);
        t2.setEditable(false);
        t3.setEditable(false);
        l4.setVisible(false) ;
        l5.setVisible(false) ;
        l6.setVisible(false) ;
        l2.setVisible(false) ;
        l3.setVisible(false) ;
        b2.setVisible(true);
        b1.setVisible(false);
        l10.setText("Press Left Mouse Button to Send Money");
        l11.setText("");

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Rev_Trust().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b1;
    private javax.swing.JButton b2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel l1;
    private javax.swing.JLabel l10;
    private javax.swing.JLabel l11;
    private javax.swing.JLabel l2;
    private javax.swing.JLabel l3;
    private javax.swing.JLabel l4;
    private javax.swing.JLabel l5;
    private javax.swing.JLabel l6;
    private javax.swing.JTextField t1;
    private javax.swing.JTextField t2;
    private javax.swing.JTextField t3;
    // End of variables declaration//GEN-END:variables

}
